<?php
/*
+---------------------------------------------------------------------------
|   Arka-Tech Web Application v2.0.0
|   ========================================
|   by Christopher Hall
|   (c) 2009 ArkahnX-Technology
|   http://arka-tech.net
|   ========================================
|   Web: http://arka-tech.net/os/
|   Time: Teus, 14 July 2009 10:44 PM
|   Release: RmVicnVhcnkgMjAwOSBMYXVuY2ggNA==
|   Email: ArkahnX@gmail.com
|   Licence Info: http://www.arka-tech.net/os/
+---------------------------------------------------------------------------
*/

if ($_REQUEST['hostname'] && strlen($_REQUEST['password']) > 2 && $_REQUEST['password'] == $_REQUEST['password2']) {
	install_do($_REQUEST['install'],$_REQUEST['allow_register'],$_REQUEST['hostname'],$_REQUEST['password']);
} else {
	install_start($_REQUEST['allow_register'],$_REQUEST['hostname'],$_REQUEST['password'],$_REQUEST['password2']);
}

function install_start($allow_register,$hostname,$password,$password2) {
	if (isset($hostname) && !$_REQUEST['hostname']) {
		$errorHostname = lang_translate('installer-install-hostname-error','You must specify a hostname!');
	}
	if (isset($password) || isset($_REQUEST['password2'])) {
		if (!$password) {
			$errorPassword = lang_translate('installer-install-passwords-required','The root password is required!');
			$password2 = '';
		} elseif (strlen($password) < 3) {
			$errorPassword = lang_translate('installer-install-passwords-characters','The root password needs to be more than three characters!');
			$password = '';
			$password2 = '';
		} elseif ($password != $password2) {
			$errorPassword = lang_translate('installer-install-passwords-notmatch','The root passwords do not match!');
			$password2 = '';
		}
	}
	
	$content = '<form action="./index.php" method="post">
	<div class="passwords">
		<div class="password_text">' . lang_translate('installer-install-password-root','Root password') . '</div>
		<input class="password" name="password" type="password" value="' . htmlspecialchars($password,ENT_QUOTES,'UTF-8') . '" />
		<div class="errorPassword">' . $errorPassword . '</div>
		<div class="password2_text">' . lang_translate('installer-install-password-retype','Retype password') . '</div>
		<input class="password2" name="password2" type="password" value="' . htmlspecialchars($password2,ENT_QUOTES,'UTF-8') . '" />
	</div>
	<div class="options">
		<div class="hostname_text">' . lang_translate('installer-install-hostname','Hostname') . '</div>
		<input class="hostname" name="hostname" type="text" value="' . htmlspecialchars($hostname,ENT_QUOTES,'UTF-8') . '" />
		<div class="errorHostname">' . $errorHostname . '</div>
		<div class="allow_register_text">' . lang_translate('installer-install-allow-creation','Allow users to create accounts') . '</div>
		<input ';
	if ($allow_register == 'on') {
		$content .= 'checked="checked" ';
	}
	$content .= 'class="allow_register" name="allow_register" type="checkbox" />
	</div>
	<div class="button"><input class="iButton" type="submit" value="' . lang_translate('installer-install-do','Install ATWA!') . '" /></div>
</form>';
	output_do($content,'install',lang_translate('installer-install-title','Please fill out the following form to install ATWA:'));
}

function install_do($install,$allow_register,$hostname,$password) {
	if ($allow_register == 'on') {
		$allow_register = 1;
	} else {
		$allow_register = 0;
	}
	
	if ($install != '2') {
		// Untar source.tar.gz
		if (TYPE_INSTALL === 1) {
			include_once(INSTALL_DIR . 'pcl-tar.php');
			if (!PclTarExtract(INSTALL_PACKAGE,'./','','tgz')) {
				output_errors(array(lang_translate('installer-install-error-extract','The install files could not be extracted. To install ATWA manually, rename "[package]" to "package.tar.gz" and extract the contents into the same directory.',array('[package]' => INSTALL_PACKAGE))));
			}
		}
		
		// Rename dir
		$hash = 'CORE' . substr(md5(uniqid(rand())),0,10);
		if (!path_move('./CORE/','./' . $hash . '/')) {
			output_errors(array(lang_translate('installer-install-error-renaming','Could not rename the directory "./CORE/". Please, rename it manually to "[hash]".',array('[hash]' => $hash))),array('./index.php?install=2&amp;hash=' . $hash,lang_translate('installer-install-continue','Continue...')),0);
		}
	} else {
		$hash = $_REQUEST['hash'];
	}
	
	// Create module files
	$xml = xml_xml2array(file_get_contents(INSTALL_DIR . 'files/modules.xml'));
	foreach ($xml['modules'][0] as $module => $files) {
		@mkdir('./' . $module . '/');
		foreach ($files[0] as $file => $content) {
			file_put_contents('./' . $module . '/' . $file,base64_decode($content[0]));
		}
	}
	
	// system.xml
	$systemxml = xml_xml2array(file_get_contents('./' . $hash . '/system/conf/system.xml'));
	$systemxml['CONFIG'][0]['ALLOW_USER_REGISTER'][0] = $allow_register;
	$systemxml['CONFIG'][0]['ATWA_HOSTNAME'][0] = $hostname;
	if (strpos(realpath('./'),'\\')) {
		$systemxml['CONFIG'][0]['OFFICE_COMMAND'][0] = 'C:\\Program Files\\OpenOffice.org 2.4\\program\\';
		$systemxml['CONFIG'][0]['OFFICE_SEPARATOR_ARG'][0] = '"';
	}
	file_put_contents('./' . $hash . '/system/conf/system.xml',xml_array2xml($systemxml));
	// root.xml
	$rootxml = xml_xml2array(file_get_contents('./' . $hash . '/accounts/rt4/root.xml'));
	$rootxml['User'][0]['password'][0] = md5($password . md5($password));
	$rootxml['User'][0]['createDate'][0] = time();
	file_put_contents('./' . $hash . '/accounts/rt4/root.xml',xml_array2xml($rootxml));
	
	// index.php
	copy(INSTALL_DIR . 'files/index.txt','./index.php');
	// settings.php
	$settingsphp = file_get_contents(INSTALL_DIR . 'files/settings.txt');
	$settingsphp = str_replace("define('REAL_OS_ROOT','CORE');","define('REAL_OS_ROOT','" . $hash . "');",$settingsphp);
	file_put_contents('./settings.php',$settingsphp);
	
	// Remove the installer
	output_remove();
}
?>